package pac;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.dom4j.Document;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.XMLWriter;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class LoginTest {

	private static final String SERVER = "http://222.200.180.9/PaperPlaneServer/";
	public static void main( String[] args ){
		try {
			URL url = new URL( SERVER + "log_in");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			
//			设置请求参数
			connection.setReadTimeout(20000);
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setDoInput(true);

			connection.connect();
//			HttpPost post = new HttpPost( SERVER + "log_in" );
//			HttpEntity entity = new BasicHttpEntity();
			
			PrintWriter writer = new PrintWriter( connection.getOutputStream() );
			
			Document document = XMLUtils.changeToXML("login", new String[]{XMLUtils.TAG_EMAIL, XMLUtils.TAG_PASSWORD}, new String[]{ "luoxwen@gmail.com", "123456"});
//			SAXWriter writer = new SAXWriter();
			document.write(writer);
			writer.flush();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			MyParseHandler handler = new MyParseHandler();
			parser.parse(connection.getInputStream(),  handler );
			int userID = handler.getUserID();
			System.out.println("Finished UserID: " + userID);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//		} catch (URISyntaxException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		} 
	}
	
	static class MyParseHandler extends DefaultHandler{

		int userID;

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			userID = Integer.parseInt( new String( ch, start, length));
		}		
		public int getUserID(){
			return userID;
		}
	}
}
